<template>
  <!-- 修改仓库 -->
  <div class="stock">
    <div class="dropdown">
      <jc-button
        :btn-arr="['reload', 'pass', 'save', 'sub', 'remove', 'list']"
        :disab-arr="[isDisabled && 'save', 'using', 'close', 'cancel', 'com1', 'com2', 'freeze']"
        :list="'/cashFlowList'"
        @save="subMarker"
        @submit="subMarker(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @del="delet()"
        @refresh="stockDetails"
      />
    </div>
    <el-tabs v-model="activeName" @tab-click="handleOther">
      <el-tab-pane :label="$t('market.info')" name="first">
        <el-form ref="purchaseRef" :model="prodValue" size="mini" label-width="100px" :disabled="isDisabled">
          <el-form-item :label="$t('shop.fnumber')" prop="fbillno">
            <el-input v-model.trim="prodValue.fbillno" size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.direction')" prop="fdirection">
            <el-select v-model.trim="prodValue.fdirection" size="mini" filterable>
              <el-option label="现金流入/增加" :value="1" />
              <el-option label="现金流出/减少" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('market.properType')" prop="fbilltypeid">
            <el-select v-model.trim="prodValue.fbilltypeid" size="mini" filterable>
              <el-option label="主表项目" :value="1" />
              <el-option label="附表项目" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('shop.name1')" style="width: 33.2%">
            <el-input v-model.trim="prodValue.fname" size="mini" style="height: 35px" />
          </el-form-item>
          <el-form-item :label="$t('market.descrip')" style="width: 33.2%">
            <el-input v-model.trim="prodValue.fdescription" size="mini" style="height: 35px" />
          </el-form-item>
          <el-form-item :label="$t('market.documentstatus')">
            <el-tag :type="prodValue.stateColor">{{ prodValue.fdocumentstatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.forbidStatu')">
            <el-tag v-if="prodValue.fforbidstatus === 'A'">{{ $t('market.normal') }}</el-tag>
            <el-tag v-else-if="prodValue.fforbidstatus === 'B'" type="danger">{{ $t('btn.disab') }}<!--禁用--></el-tag>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="log">
        <jc-marker
          ref="log"
          :fnumber="prodValue.fbillno"
          other-height="68vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  deleteCashFlowProject,
  notreviewCashFlowProject, notsubmitCashFlowProject, queryCashFlowProjectDetails,
  reviewCashFlowProject, submitCashFlowProject, updateCashFlowProject
} from '@/api/basicManagement/cashFlow'
import { toMxBmina } from '@/components/ToMxamineState'
import jcMarker from '@/components/marker'
import jcButton from '@/components/Button'

export default {
  components: {
    jcMarker,
    jcButton
  },
  data() {
    return {
      isDisabled: false,
      activeName: 'first',
      prodValue: {},
      otherUrlObject: {}
    }
  },
  created() {
    this.stockDetails()
  },
  methods: {
    // 保存
    subMarker(ev) {
      const { fbillno, fdirection, fbilltypeid, fname, fdescription, fid } = this.prodValue
      const DATA = {
        fbillno, fdirection, fbilltypeid, fname, fdescription, fid
      }
      updateCashFlowProject(DATA).then(res => {
        if (res.code === 0) {
          ev === 1 ? this.submit(1) : this.stockDetails()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        }
      })
    },
    // 详情
    stockDetails() {
      queryCashFlowProjectDetails({ fid: this.$route.params.id }).then(res => {
        if (res.code === 0) {
          this.prodValue = toMxBmina(res.data)
          this.isDisabled = this.prodValue.fdocumentstatus === this.$t('market.state1') ||
            this.prodValue.fdocumentstatus === this.$t('market.state2')
          this.otherUrlObject = res.data
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        }
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.prodValue.fbillno] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.stockDetails()
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? submitCashFlowProject : notsubmitCashFlowProject
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? reviewCashFlowProject : notreviewCashFlowProject
      this.toExamineFun(api)
    },
    delet() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const DATA = { Numbers: [this.prodValue.fbillno] }
        deleteCashFlowProject(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/cashFlowAdd' })
        })
      })
    },
    // 操作记录
    handleOther(tab) {
      if (tab.name === 'log') {
        this.$refs.log.otherlog()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.stock {
  padding: 10px;
  background: #fff;
  min-height: calc(100vh - 100px);
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;

    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 35px;
      width: 20%;
      margin-bottom: 5px;
      .btnType {
        margin-right: 20px;
      }

      .el-date-editor.el-input {
        width: 100% !important;
      }

      .el-select {
        width: 100%;
      }
    }
  }
}
</style>
